import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.image.BufferedImage;
import java.io.File;

import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;

import javax.swing.Timer;

public class SmoothMoves extends JComponent implements ActionListener {

	BufferedImage image = null;
	Image bus1Image, bus2Image, bus3Image, walkImage;
	static int imageW = 100;
	static int imageH = 150;
	
	boolean active;

	ArrayList<Point> currentPoint;
	ArrayList<Point> point1;
	ArrayList<Point> point2;

	int moveX = move1x;
	int moveY = move1y;

	int index = 0;

	static int move1x = 629;
	static int move1y = 201;
	static int move2x = 629;
	static int move2y = 213;
	static int move3x = 593;
	static int move3y = 213;
	static int move4y = 360;

	final static int CYCLE_TIME = 5000;
	int currentdelay = 10;
	Timer timer = null;
	long cycleStart;

	static float fraction = 0;

	public SmoothMoves() {
		active = false;
		
		// Load image data
		try {
				File sourceimage = new File("imgs/bus_icon_s1.png");
				bus1Image  = ImageIO.read(sourceimage);
				sourceimage = new File("imgs/bus_icon_s2.png");
				bus2Image = ImageIO.read(sourceimage);
				sourceimage = new File("imgs/bus_icon_s3.png");
				bus3Image = ImageIO.read(sourceimage);
				sourceimage = new File("imgs/bus_icon_s4.png");
				walkImage = ImageIO.read(sourceimage);
		} catch (Exception e) {
			System.out.println("Image does not exist!!");
		}
		
		// Set up coordinates

		point1 = new ArrayList<Point>();
		point2 = new ArrayList<Point>();

		point1.add(new Point(649, 172, "bus1"));
		point1.add(new Point(649, 184, "bus1"));
		point1.add(new Point(613, 184, "bus1"));
		point1.add(new Point(613, 331, "bus1"));
		point1.add(new Point(531, 331, "bus1"));
		point1.add(new Point(531, 541, "bus1"));
		point1.add(new Point(579, 541, "bus1"));
		point1.add(new Point(579, 620, "bus1"));
		point1.add(new Point(579, 620, "bus1"));
		
		point2.add(new Point(951, 528, "bus3"));
		point2.add(new Point(951, 504, "bus3"));
		point2.add(new Point(636, 504, "walk"));
		point2.add(new Point(585, 504, "walk"));
		point2.add(new Point(585, 440, "bus2"));
		point2.add(new Point(585, 285, "bus2"));
		point2.add(new Point(253, 285, "bus2"));
		point2.add(new Point(253, 270, "bus2"));
		point2.add(new Point(253, 270, "bus2"));
		
		currentPoint = point1;

		cycleStart = System.nanoTime() / 1000000;
		startTimer(currentdelay);

		
	}

	public void paintComponent(Graphics g) {

		GraphicsConfiguration gc = getGraphicsConfiguration();
		image = gc.createCompatibleImage(imageW, imageH, Transparency.OPAQUE);
		Graphics2D gImg = image.createGraphics();
	
		if (currentPoint.get(index).getType() == "bus1") {
			g.drawImage(bus1Image, moveX, moveY, null);
		} else if (currentPoint.get(index).getType() == "bus2") {
			g.drawImage(bus2Image, moveX, moveY, null);
		} else if (currentPoint.get(index).getType() == "bus3") {
			g.drawImage(bus3Image, moveX, moveY, null);
		} else if (currentPoint.get(index).getType() == "walk") {
			g.drawImage(walkImage, moveX, moveY, null);
		}

		gImg.dispose();

	}

	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource().equals(timer))
		{
			float a = .005f;
	
			if (fraction <= 1) {
				fraction = a + fraction;
	
			} else {
				fraction = 0;
				index++;	
			}
			
			animate(fraction, index);
	
			if (index > currentPoint.size() - 3) {
				index = 0;
			}
		}
	}

	public void animate(float fraction, int index) {

		moveY = currentPoint.get(index).getY()
				+ (int) (fraction * (float) (currentPoint.get(index + 1).getY() - currentPoint
						.get(index).getY()));
		moveX = currentPoint.get(index).getX()
				+ (int) (fraction * (float) (currentPoint.get(index + 1).getX() - currentPoint
						.get(index).getX()));

		repaint();

	}

	private void startTimer(int delay) {
		timer = new Timer(delay, this);
		timer.start();
	}
	
	public boolean isActive()
	{
		return active;
	}

	public void setActive(boolean a)
	{
		active = a;
	}
	
	public void setRoute(int i)
	{
		if (i == 0) currentPoint = point2;
		if (i == 1) currentPoint = point1;
	}
	
	public void restart()
	{
		index = 0;
		fraction = 0;
	}
}
